import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Login2 from '../components/Login2.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/Users.vue'
import Rights from '../components/power/Rights'
import Role from '../components/power/Role'
import Cate from '../components/goods/Cate'
import Params from '../components/goods/Params'
import GoodList from '../components/goods/List'
import Add from '../components/goods/Add'
import Order from '../components/order/Order'
import Code from '../components/Code.vue'
import Report from '../components/report/Report.vue'



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/login2',
    name: 'Login2',
    component: Login2
  },
  {
    path: '/code',
    name: 'Code',
    component: Code
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'User',
        component: User
      },
      {
        path:'/roles',
        name: 'Role',
        component: Role
      },
      {
        path:'/rights',
        name: 'Rights',
        component: Rights
      },{
        path:'/categories',
        name: 'Cate',
        component: Cate
      },{
        path:'/params',
        name: 'Params',
        component: Params
      },{
        path:'/goods',
        name: 'GoodList',
        component: GoodList
      },{
        path:'/goods/add',
        name: 'Add',
        component: Add
      },{
        path:'/orders',
        name: 'Order',
        component: Order
      },{
        path:'/reports',
        name: 'Report',
        component: Report
      }
    ]
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 一个函数，表示放行
  // next() 放行   next('/login)强制跳转

  if (to.path == '/login' || to.path == '/login2' ||to.path == '/code') return next()
  // 获取缓存token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
